﻿// TcpIpDlg.cpp: 实现文件
//

#include "pch.h"
#include "SP.h"
#include "TcpIpDlg.h"
#include "afxdialogex.h"


// TcpIpDlg 对话框

IMPLEMENT_DYNAMIC(TcpIpDlg, CDialogEx)

TcpIpDlg::TcpIpDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DLG_TCPIP, pParent)
	, m_nPort(0)
	, Gongxu(_T(""))
	, Shebei(_T(""))
	, UseMesFlag(FALSE)
{

}

TcpIpDlg::~TcpIpDlg()
{
}

void TcpIpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS1, m_ipAdd);
	DDX_Text(pDX, IDC_EDIT1, m_nPort);
	DDX_Text(pDX, IDC_EDIT2, Gongxu);
	DDX_Text(pDX, IDC_EDIT3, Shebei);
	DDX_Check(pDX, IDC_CHECK1, UseMesFlag);
}

BOOL TcpIpDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	m_ipAdd.SetWindowText(Manager.m_cstrIPMes);
	m_nPort = Manager.m_nPortMes;
	UseMesFlag = Manager.MesFlag;

	Gongxu = Manager.Gongxu;
	Shebei = Manager.Shebei;

	UpdateData(FALSE);
	return TRUE;
}


BEGIN_MESSAGE_MAP(TcpIpDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BTN_SAVE, &TcpIpDlg::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BUTTON1, &TcpIpDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// TcpIpDlg 消息处理程序


void TcpIpDlg::OnBnClickedBtnSave()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	m_ipAdd.GetWindowText(Manager.m_cstrIPMes);
	Manager.m_nPortMes = m_nPort;
	Manager.MesFlag = UseMesFlag;

	Manager.Gongxu = Gongxu;
	Manager.Shebei = Shebei;

	CString cstr;

	Manager.m_cMesConfigIni.SaveConfigIni(L"MES", L"m_cstrIPMes", Manager.m_cstrIPMes);
	cstr.Format(L"%d", Manager.m_nPortMes);
	Manager.m_cMesConfigIni.SaveConfigIni(L"MES", L"m_nPortMes", cstr);
	cstr.Format(L"%d", Manager.MesFlag);
	Manager.m_cMesConfigIni.SaveConfigIni(L"MES", L"MesFlag", cstr);

	Manager.m_cMesConfigIni.SaveConfigIni(L"MES", L"Gongxu", Manager.Gongxu);
	Manager.m_cMesConfigIni.SaveConfigIni(L"MES", L"Shebei", Manager.Shebei);

	
	Manager.GetLog()->DisplayAndSaveLog(L"MES通讯保存数据完成", LV_EVENT);
}


void TcpIpDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	Manager.MesOut("123", true);

	//int result = 0;
	//CString cstr;

	//if (Manager.MesFlag) {
	//	USES_CONVERSION;
	//	char mes_ip[260];
	//	char tempTxt[260];
	//	//接口名：Scan_Check
	//	//接口参数：param = { "procode":"","dmcode" : "","invcode" : "","fcode" : "" }
	//	sprintf(mes_ip, "http://%s:%d/Scan_Check?param={\"procode\":\"%s\",\"dmcode\":\"%s\",\"invcode\":\"%s%s%s\"}",
	//		T2A(Manager.m_cstrIPMes), Manager.m_nPortMes, T2A(Manager.Gongxu), T2A(Manager.Shebei), Manager.SN2, (Manager.PassPCBSNFlag) ? "" : "@", (Manager.PassPCBSNFlag) ? "" : Manager.SN);
	//	sprintf(tempTxt, "");

	//	std::string Str = Manager.RequestJsonInfo(mes_ip, Hr_Post, tempTxt, "Content-Type:application/json", TRUE);
	//	cstr.Format(L"queryCarrierTrayInfo:%s", A2T(Str.c_str()));
	//	Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
	//	Json::Reader reader;                                    //解析json用Json::Reader
	//	Json::Value value;                                        //可以代表任意类型
	//	if (!reader.parse(Str, value))
	//	{
	//		Manager.GetLog()->DisplayAndSaveLog(L"parse error", LV_EVENT);
	//		return;
	//		result = -1;
	//	}
	//	if (value["status"].asBool() == FALSE) {
	//		Manager.GetLog()->DisplayAndSaveLog(L"Mes 返回 status fail", LV_EVENT);
	//		result = -2;
	//		return;
	//	}
	//}
}
